//
//  MyBonusTableViewController.m
//  1243
//
//  Created by 袖手旁觀 on 16/4/7.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "MyBonusTableViewController.h"
#import "fitstCellTableViewCell.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"
#import "MyBonusTableViewCell.h"



@interface MyBonusTableViewController ()

@property (nonatomic,strong) NSArray *array;

@property (nonatomic,strong) NSArray *firstCell;

@property (nonatomic,strong) NSArray *secondCell;

@property (nonatomic,strong) NSArray *rowFirstCell;

@property (nonatomic,strong) NSArray *rowSecondCell;

@property (nonatomic,strong)NSDictionary *DataDict;

@property(nonatomic,strong)NSArray *myBonusArray;
@end

@implementation MyBonusTableViewController

#pragma mark 懒加载
-(NSArray *)firstCell {
    if (!_firstCell) {
        _firstCell = @[@"序号",@"购买时间",@"姓名",@"第1周",@"第2周",@"第3周",@"第4周",@"第5周"];
    }
    return _firstCell;
}

- (NSArray *)secondCell {
    if (!_secondCell) {
        _secondCell = @[@"序号",@"购买时间",@"姓名",@"第6周",@"第7周",@"第8周",@"第9周",@"第10周"];
    }
    return _secondCell;
}

- (NSArray *)array {
    if (!_array) {
        _array = @[
                   @{
                       @"title":@"已产生的奖金",
                       @"num":@"HadGiveMoney"
                       },
                   @{
                       @"title":@"未产生的奖金",
                       @"num":@"NotGiveMoney"
                       }
                   ];
    }
    return _array;
}

- (NSArray *)rowFirstCell {
    if (!_rowFirstCell) {
        _rowFirstCell = @[
                       @[@"500",@"450",@"900",@"1370",@"1080"],
                       @[@"500",@"450",@"900",@"1370",@"1080"],
                       @[@"500",@"450",@"900",@"1370",@"1080"],
                       @[@"500",@"450",@"900",@"1370",@"1080"],
                       @[@"500",@"450",@"900",@"1370",@"1080"]
                       ];
    }
    return _rowFirstCell;
}

- (NSArray *)rowSecondCell {
    if (!_rowSecondCell) {
        _rowSecondCell = @[
                           @[@"1080",@"1080",@"1080",@"1080",@"1080"],
                           @[@"1080",@"1080",@"1080",@"1080",@"1080"],
                           @[@"1080",@"1080",@"1080",@"1080",@"1080"],
                           @[@"1080",@"1080",@"1080",@"1080",@"1080"],
                           @[@"1080",@"1080",@"1080",@"1080",@"1080"]
                           ];
    }
    return _rowSecondCell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.tableView.rowHeight = 40;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.navigationItem.backBarButtonItem setTintColor:[UIColor whiteColor]];
    
//    self.tableView.style = UITableViewStyleGrouped;
    
    [self loadUserBonus];
    
    self.title = @"我的积分";
}

#pragma mark Header Footer 高
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    } else {
        return 20;
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 0;
    } else if (section == 2) {
        return 0;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 40;
    } else {
        return 175;
    }
}

#pragma mark tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 2;
    }
    return self.myBonusArray.count;
//    return 0;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.textLabel.text = [self.array[indexPath.row] objectForKey:@"title"];
        
        NSString *string = [self.array[indexPath.row] objectForKey:@"num"];
        
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f",[[self.DataDict objectForKey:string] floatValue]];
        cell.detailTextLabel.textColor = [UIColor orangeColor];
        
        return cell;
    } else if (indexPath.section == 1) {
        MyBonusTableViewCell *MyBonus = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (!MyBonus) {
            MyBonus = [[[NSBundle mainBundle] loadNibNamed:@"MyBonusTableViewCell" owner:nil options:nil] lastObject];
            MyBonus.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        NSString *name = [self.myBonusArray[indexPath.row] objectForKey:@"BuyerName"];
        NSLog(@"%@",name);
        MyBonus.BuyerNameLabel.text = [NSString stringWithFormat:@"姓名: %@",name];
        
        NSString *time = [self.myBonusArray[indexPath.row] objectForKey:@"CreateTime"];
        
        time = [time stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
        
        MyBonus.CreatTimeLabel.text = [NSString stringWithFormat:@"创建时间: %@",time];
        
        NSString *PhoneNumber = [self.myBonusArray[indexPath.row] objectForKey:@"UserName"];
        
        MyBonus.UserNameLabel.text = [NSString stringWithFormat:@"电话: %@",PhoneNumber];
        
        NSString *weeks = [self.myBonusArray[indexPath.row] objectForKey:@"Weeks"];
        
        MyBonus.WeeksLabel.text = [NSString stringWithFormat:@"奖励周数: %@",weeks];
        
        MyBonus.bonusLabel.text = [NSString stringWithFormat:@"奖金: %@",[self.myBonusArray[indexPath.row] objectForKey:@"Unumber"]];
        
        return MyBonus;
    }
    return nil;
}


// tableViewCell 图标类型展示
/*
 //
 //            fitstCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fitstContentViewCell"];
 //
 //            if (!cell) {
 //                cell = [[fitstCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"fitstContentViewCell"];
 //                cell.selectionStyle = UITableViewCellSelectionStyleNone;
 //            }
 //            cell.firstCellLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
 //            cell.firstCellLabel.textAlignment = NSTextAlignmentCenter;
 //            NSString *timeString = [self.myBonusArray[indexPath.row] objectForKey:@"CreateTime"];
 //
 //            cell.timeCellLabel.text = [timeString stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
 //            cell.nameLabel.text = [self.myBonusArray[indexPath.row] objectForKey:@"BuyerName"];
 //            cell.timeCellLabel.numberOfLines = 0;
 //
 //
 //            int changeColorNumber = [[self.myBonusArray[indexPath.row] objectForKey:@"Weeks"] intValue];
 //
 //            cell.changeCellColor = changeColorNumber;
 //
 //            cell.cellButtonCount = 5;
 //            cell.cellDataArray = self.rowFirstCell;
 //            [cell.contentView addSubview:cell.firstCellLabel];
 //            [cell.contentView addSubview:cell.timeCellLabel];
 //            [cell.contentView addSubview:cell.buttonArray];
 //            [cell.contentView addSubview:cell.nameLabel];
 //            return cell;
 //
 //    } else {
 //
 //
 //
 //        fitstCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondContentViewCell"];
 //
 //        int changeColorNumber = [[self.myBonusArray[indexPath.row] objectForKey:@"Weeks"] intValue];
 //
 //        if (changeColorNumber > 5) {
 //            cell.changeCellColor = changeColorNumber - 5;
 //            cell.isCellColor = YES;
 //        }
 //
 //
 //        if (!cell) {
 //            cell = [[fitstCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secondContentViewCell"];
 //            cell.selectionStyle = UITableViewCellSelectionStyleNone;
 //        }
 //        cell.firstCellLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
 //        cell.firstCellLabel.textAlignment = NSTextAlignmentCenter;
 //        NSString *timeString = [self.myBonusArray[indexPath.row] objectForKey:@"CreateTime"];
 //
 //        cell.timeCellLabel.text = [timeString stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
 //        cell.nameLabel.text = [self.myBonusArray[indexPath.row] objectForKey:@"BuyerName"];
 //        cell.timeCellLabel.numberOfLines = 0;
 //
 //        cell.cellButtonCount = 5;
 //        cell.cellDataArray = self.rowSecondCell;
 //        [cell.contentView addSubview:cell.firstCellLabel];
 //        [cell.contentView addSubview:cell.timeCellLabel];
 //        [cell.contentView addSubview:cell.nameLabel];
 //        [cell.contentView addSubview:cell.buttonArray];
 //        return cell;
 */

#pragma mark tableViewHeaderFooterView
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    if (section == 1) {
//        return [self addFirstCellAndArray:self.firstCell];
//    } else if (section == 2) {
//        return [self addFirstCellAndArray:self.secondCell];
//    }
//    return nil;
//}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
//        return [self lastSectionTableFooterView];
    }
    return nil;
}

#pragma mark 第一行cell
- (UIView *)addFirstCellAndArray:(NSArray *)cellArray{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WEITH, 35)];
    view.backgroundColor = [UIColor colorWithRed:176 / 255.0 green:189 / 255.0 blue:222 / 255.0 alpha:1];
    
    for (int i = 0;i < 8; i++) {
        if (i >= cellArray.count) {
            break;
        }
        
        CGFloat width = WEITH / 8;
        UIButton *button = [[UIButton alloc] init];
        [button setFrame:CGRectMake(width * i, 5, width, view.bounds.size.height)];
        [button setTitle:[NSString stringWithFormat:@"%@",cellArray[i]] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.titleLabel.numberOfLines = 0;
        
        if (i == 1) {
            [button setFrame:CGRectMake(width, 8, width - 5, 25)];
        }
        [view addSubview:button];
    }
    
    
    return view;
}

//- (UIView *)lastSectionTableFooterView {
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 80)];
//    view.backgroundColor = [UIColor colorWithRed:228 / 255.0 green:229 / 255.0 blue:233 / 255.0 alpha:1];
//    
//    UIButton *nextPageButton = [[UIButton alloc] initWithFrame:CGRectMake((WIDTH - 100) / 2, (80 - 35) / 2, 100, 35)];
//    [nextPageButton setTitle:@"下一页" forState:UIControlStateNormal];
//    [nextPageButton setBackgroundColor:[UIColor orangeColor]];
//    [nextPageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [nextPageButton addTarget:self action:@selector(didClickNextPageButton) forControlEvents:UIControlEventTouchUpInside];
//    nextPageButton.layer.cornerRadius = 5;
//    nextPageButton.layer.masksToBounds = YES;
//    
//    [view addSubview:nextPageButton];
//    
//    return view;
//}

- (void)didClickNextPageButton {
    NSLog(@"%s",__func__);
}

- (void)loadUserBonus {
    
    NSDictionary *userDict = [ReadLoginData readLoginData];
    
    
    
    NSDictionary *dict = @{
                           @"UserNumber":[userDict objectForKey:@"Number"]
//                           @"UserNumber":@"1001"
                           };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"MYBOUNS" completeBlock:^(id object) {
       
        NSLog(@"%@",object); 
        
        NSString *string = [object objectForKey:@"BackData"];
        
        self.DataDict = [NetWorkTools dictionaryWithJsonString:string];
        
        NSString *arrayString = [self.DataDict objectForKey:@"Data"];
        
        self.myBonusArray = [NetWorkTools arrayWithJsonString:arrayString];
        
        NSLog(@"%@",self.myBonusArray);
        
        [self.tableView reloadData];
    }];
}

@end
